package com.ww.learn.oop.extend

open class Vehicle(val year: Int, open var color: String) {

    open val km = 0

    final override fun toString() = "year: $year color: $color, KM: $km"

    fun repaint(newColor: String) {
        color = newColor
    }
}

open class Car2(year: Int, color: String) : Vehicle(year, color) {
    override var km: Int = 0
        set(value) {
            if (value < 1) {
                throw RuntimeException("can't set negative value")
            }
            field = value
        }

    fun drive(distance: Int) {
        km += distance
    }
}

class FamilyCar(year: Int, color: String) : Car2(year, color) {
    override var color: String
        get() = super.color
        set(value) {
            if (value.isEmpty()) {
                throw RuntimeException("Color required")
            }
            super.color = value
        }
}

fun main() {
//    val car = Car2(2019, "Orange")
//    println(car.year)
//    println(car.color)
//    car.drive(10)
//    println(car)
//
//    try {
//        car.drive(-30)
//    } catch (e: Exception) {
//        println(e.message)
//    }

    val familyCar = FamilyCar(2019, "Green")
    println(familyCar.color)

    try {
        familyCar.repaint("")
    } catch (e: Exception) {
        println(e.message)
    }
}